var html = '';

$(document).ready(function() {
	setInterval("document.getElementById('currentTimeDiv').innerHTML = new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
});
/**
 * 点击菜单栏
 * @param flag 导航菜单栏中相应的位置
 */
function menuClick(flag) {
	menuSelected(flag);
	switch (flag) {
		case 1:
			indexMenu();
			break;
		case 2:
			articleList('', 0);
			break;
		case 3:
			photoMenu();
			break;
		case 4:
			msgMenu();
			break;
		case 5:
			resList('', 0);
			break;
		case 6:
			contractMenu();
			break;
	
		default:
			break;
	}
}
//相片菜单
function photoMenu() {
	toast("TODO");
}
//留言板菜单
function msgMenu() {
	toast("TODO");
}
//资源菜单
function resList(tagId, pageIndex) {
//	toast("TODO");
	$.ajax({
		url: './action/res/getResList.php',
		type: 'POST',
		data: {
			tagId : tagId,
			pageIndex : pageIndex
		},
		dataType: ($.browser.msie) ? 'text' : 'xml',
		success: function (xmlData) {
			var xml;
            if( typeof xmlData == 'string'){
                xml = new ActiveXObject("Microsoft.XMLDOM");  
                xml.async = false;  
                xml.loadXML(xmlData);  
            } else {  
                xml = xmlData;   
            }
			var contentDom = $("#content");
			contentDom.html('');//删除内容
			
			var totalNum = $(xml).find("ress").attr("total");
			var pageIndex = $(xml).find("ress").attr("index");
			var pageSize = $(xml).find("ress").attr("pageSize");
			var currentPageSize = 0;
			
			var htmlContent = "";
			htmlContent = htmlContent +  '<h1>Resource List</h1><ul>';
			$(xml).find("res").each(function(i) {
				currentPageSize ++;
				var id = $(this).attr("id");
				var name = $(this).children("name").text();
				var path = $(this).children("path").text();
				var time = $(this).children("time").text();
				var type = $(this).children("type").text();
				var size = $(this).children("size").text();
				var top = $(this).children("top").text();
				var tagId = $(this).children("tagId").text();
				var tagName = $(this).children("tagName").text();
				var num = $(this).children("num").text();
				if (name.length > 15) {
					name = name.substr(0, 15) + '...';
				}
				name = '<a href="' + path + '" onclick="javascript:downLoadRes(' + id + ', \'' + path + '\');" target="_blank">' + name + '</a>';
				if (top == '1') {
					name = '<red>[HOT]</red>&nbsp;&nbsp;' + name;
				}
				if (size > 1024) {
					size = size / 1024.0;
					if (size > 1024) {
						size = size / 1024.0;
						if (size.toString().length > 4) {
							size = size.toString().substr(0, 4);
						}
						size = size + 'MB';
					}
					else {
						if (size.toString().length > 4) {
							size = size.toString().substr(0, 4);
						}
						size = size + 'KB';
					}
				}
				else {
					if (size.toString().length > 4) {
						size = size.toString().substr(0, 4);
					}
					size = size + 'B';
				}
				name = name + '&nbsp;&nbsp;[' + size + ']';
				name = name + '&nbsp;&nbsp;<tag onclick="javascript:resList(' + tagId + ','+ pageIndex +')">&nbsp;' + tagName + '&nbsp;</tag>';
				var onlineView = (type == '1' && $.browser.webkit) ? 'View Online' : '';
				htmlContent = htmlContent + '<li>' + name + '<div style="float:right; margin-right:10px;">&nbsp;[<red id="res_' + id + '">'+ num +'</red> load]&nbsp;&nbsp;<a href="javascript:onlineView(\'' + path + '\');">' + onlineView + '</a>&nbsp;&nbsp;&nbsp;&nbsp;' + time + '</div>' + '</li>';
			});
			htmlContent = htmlContent + '</ul>';
			//加入分页信息
			htmlContent = htmlContent + getPagingStr(totalNum, pageIndex, pageSize, currentPageSize, tagId, 'resList');
			
			contentDom.append(htmlContent);
		},
		error: function() {
			toast('error');
		}
	});
}

function sendArticleMsg(id) {
	var name = $("form div[.form_settings] p input[name='name']").val();
	var content = $("form div[.form_settings] p textarea[name='content']").val();
	if (name == null || trim(name) == '') {
		toast('Nick Name 不能为空,亲~');
		return;
	}
	else if(name.length > 20) {
		toast('Nick Name短一点会死啊~');
		return;
	}
	
	if (content == null || trim(content) == '') {
		toast('不说话者格杀勿论！');
		return;
	}
	else if(content.length > 400) {
		toast('精简的言语更能体现你的智慧！');
		return;
	}
	var articleId = id;
	$.ajax({
		url: './action/articleMsg/addArticleMsg.php',
		type: 'POST',
		data: {
			articleId: articleId,
			content: content,
			name: name
		},
		dataType: ($.browser.msie) ? 'text' : 'xml',
		success: function (xmlData) {
			var xml;
            if( typeof xmlData == 'string'){
                xml = new ActiveXObject("Microsoft.XMLDOM");  
                xml.async = false;  
                xml.loadXML(xmlData);  
            } else {  
                xml = xmlData;   
            }
            var result = $(xml).find("result").text(); 
            if (result == '0') {
				$("form div[.form_settings] p input[name='name']").val('');
				$("form div[.form_settings] p textarea[name='content']").val('');
				
				var msgLength = $(".aMsgBlock").length;
				var currTime = getDate();
				if (msgLength > 0) {
					var msgHtml =
						'<div class="aMsgBlock">' +
							'<div style="float:left;"> FLOOR ' + (msgLength + 1) + '   &nbsp;&nbsp;&nbsp; [<a> ' + name + ' </a>] said ' + '</div>' +
							'<div style="float:right;"> at <a>' + currTime + '</a></div><p />' +
							'<p>' + content + '</p>' +
						'</div>';
					$(".aMsgBlock:last").after(msgHtml);
				}
				else {
					var msgHtml =
						'<div class="aMsgBlock">' +
						'<div style="float:left;"> FLOOR 1   &nbsp;&nbsp;&nbsp; [<a> ' + name + ' </a>] said ' + '</div>' +
						'<div style="float:right;"> at <a>' + currTime + '</a></div><p />' +
						'<p>' + content + '</p>' +
					'</div>';
					$(".seperator:first").after(msgHtml);
				}
			}
            else if(result == '-1') {
            	toast("fail");
            }
            else if(parseInt(result) > 0) {
            	toast("歇会吧，孩子！再过" + result + "秒你才能留言~");
            }
		},
		error: function() {
			toast('error');
		}
	});
}
//发送一个联系
function sendContract() {
	var name = $("form div[.form_settings] p input[name='your_name']").val();
	var email = $("form div[.form_settings] p input[name='your_email']").val();
	var content = $("form div[.form_settings] p textarea[name='your_enquiry']").val();
	if (name == null || trim(name) == '') {
		toast('Nick Name 不能为空,亲~');
		return;
	}
	else if(name.length > 20) {
		toast('Nick Name短一点会死啊~');
		return;
	}
	if (email == null || trim(email) == '') {
		toast('Email不能为空,孩子~');
		return;
	}
	else if(trim(email).length > 30) {
		toast('你是Email是不是太长了?太难记了~');
		return;
	}
	if (! isEmail(email)) {
		toast('我没有见过你这样的邮箱,你真牛逼啊~');
		return;
	}
	
	if (content == null || trim(content) == '') {
		toast('不说话者格杀勿论！');
		return;
	}
	else if(content.length > 400) {
		toast('精简的言语更能体现你的智慧！');
		return;
	}
	$.ajax({
		url: './action/contract/addContract.php',
		type: 'POST',
		data: {
			name: name,
			content: content,
			email: email
		},
		dataType: ($.browser.msie) ? 'text' : 'xml',
		success: function (xmlData) {
			var xml;
            if( typeof xmlData == 'string'){
                xml = new ActiveXObject("Microsoft.XMLDOM");  
                xml.async = false;  
                xml.loadXML(xmlData);  
            } else {  
                xml = xmlData;   
            }
            var result = $(xml).find("result").text();
            if (result == '0') {
            	$("form div[.form_settings] p input[name='your_name']").val('');
            	$("form div[.form_settings] p input[name='your_email']").val('');
            	$("form div[.form_settings] p textarea[name='your_enquiry']").val('');
            	toast("发送成功,我看到后会给你回复的~");
            }
            
            else if(result == '-1') {
            	toast("fail");
            }
            else if(parseInt(result) > 0) {
            	toast("歇会吧，孩子！再过" + result + "秒你才能操作~");
            }
		}
	});
}

function getDate() {
	var d = new Date();
	var vYear = d.getFullYear();
	var vMon = d.getMonth() + 1;
	var vDay = d.getDate();
	var h = d.getHours(); 
	var m = d.getMinutes(); 
	var se = d.getSeconds(); 
	s = vYear+'-'+(vMon<10 ? "0" + vMon : vMon)+'-'+(vDay<10 ? "0"+ vDay : vDay)+' '+(h<10 ? "0"+ h : h)+':'+(m<10 ? "0" + m : m)+':'+(se<10 ? "0" +se : se);
	return s;//输出时间
}
//删除左右两端的空格
function trim(str){  
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
//提示框
function toast(text) {
	noty({
		"text":text,
		"theme":"noty_theme_facebook",
		"layout":"center",
		"type":"alert",
		"animateOpen":{"height":"toggle"},
		"animateClose":{"height":"toggle"},
		"speed":500,
		"timeout":3000,
		"closeButton":true,
		"closeOnSelfClick":true,
		"closeOnSelfOver":false,
		"modal":true
	});
}

function isEmail(email) {
	return email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
}